package com.ziq.zcamp.community

import android.annotation.SuppressLint
import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView
import com.ziq.base.extension.BindingViewHolder
import com.ziq.base.extension.click
import com.ziq.base.extension.createBindingViewHolder
import com.ziq.zcamp.R
import com.ziq.zcamp.bean.user.UserRelationListItem
import com.ziq.zcamp.constant.FollowRelation
import com.ziq.zcamp.databinding.ItemCommunityFollowBinding
import com.ziq.zcamp.dialog.AlertDialog
import com.ziq.zcamp.manager.GlobalContextManager
import com.ziq.zcamp.manager.UserManager
import com.ziq.zcamp.mine.PersonalHomePageActivity

class CommunityFollowAdapter(val source:String) : RecyclerView.Adapter<BindingViewHolder<ItemCommunityFollowBinding>>() {

    init {
        setHasStableIds(true)
    }

    var onRelationUpdateClick : (()->Unit)? = null

    private var dataList = mutableListOf<UserRelationListItem>()

    fun getData() : MutableList<UserRelationListItem>{
        return dataList
    }

    @SuppressLint("NotifyDataSetChanged")
    fun setData(list: MutableList<UserRelationListItem>) {
        dataList = list
        notifyDataSetChanged()
    }

    override fun getItemCount(): Int {
        return dataList.size
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): BindingViewHolder<ItemCommunityFollowBinding> {
        return createBindingViewHolder(parent)
    }

    override fun onBindViewHolder(
        holder: BindingViewHolder<ItemCommunityFollowBinding>,
        position: Int
    ) {
        val item = dataList[position]
        holder.binding.name.text = "${item.user?.nick_name}"
        holder.binding.avator.setImageURI("${item.user?.header_img}")
        if(item.content?.isNotEmpty() == true){
            holder.binding.description.visibility = View.VISIBLE
            holder.binding.description.text = "${item.content}"
        }else {
            holder.binding.description.visibility = View.GONE
        }

        if(position == dataList.size - 1){
            holder.binding.line.visibility = View.GONE
        } else {
            holder.binding.line.visibility = View.VISIBLE
        }

        updateRelation(item.relation?:"", holder)
        holder.binding.btnFollow.click {
            UserManager.instance.apply {
                if (isLogin.value == true) {
                    if (holder.binding.btnFollow.isSelected) {
                        onUnFollow(holder.binding.btnFollow.context, item, holder)
                    } else {
                        updateFollowRelationForFollow(item, holder)
                        followUser(item.user?.id?:0, onFail = {
                            updateFollowRelationForUnFollow(item, holder)
                            Toast.makeText(GlobalContextManager.appContext!!, it, Toast.LENGTH_SHORT).show()
                        })
                    }
                }
            }
        }
        holder.itemView.click {
            PersonalHomePageActivity.open(GlobalContextManager.appContext!!, item.user?.id?:0)
        }
    }

    private fun onUnFollow(context: Context, item : UserRelationListItem, holder: BindingViewHolder<ItemCommunityFollowBinding>){
        AlertDialog(context)
            .setTitle {
                it.append(GlobalContextManager.getString(R.string.are_you_sure_unfollow))
            }
            .setCancelButton(text = {it.append(GlobalContextManager.getString(R.string.cancel))}, action = {it.dismiss()})
            .setConfirmButton(text = {it.append(GlobalContextManager.getString(R.string.confirm))}, action = { dialog ->
                dialog.dismiss()
                updateFollowRelationForUnFollow(item, holder)
                UserManager.instance.unfollowUser(item.user?.id?:0, onFail = {
                    updateFollowRelationForFollow(item, holder)
                    Toast.makeText(GlobalContextManager.appContext!!, it, Toast.LENGTH_SHORT).show()
                })
            })
            .show()
    }

    private fun updateRelation(relation : String, holder: BindingViewHolder<ItemCommunityFollowBinding>){
        FollowRelation.updateRelationUI(relation,
            callback = {isVisible, isFollowing, statusText, statusTextColor ->
                holder.binding.btnFollow.visibility = if (isVisible) View.VISIBLE else View.GONE
                holder.binding.btnFollow.isSelected = isFollowing
                holder.binding.tvFollow.setText(statusText)
                holder.binding.tvFollow.setTextColor(statusTextColor)
                if(statusText == R.string.follow){
                    holder.binding.ivAdd.visibility = View.VISIBLE
                } else {
                    holder.binding.ivAdd.visibility = View.GONE
                }
            })
    }

    private fun updateFollowRelationForFollow(item : UserRelationListItem, holder: BindingViewHolder<ItemCommunityFollowBinding>){
        item.relation = FollowRelation.getRelationForFollow(item.relation?:"")
        updateRelation(item.relation!!, holder)
        onRelationUpdateClick?.invoke()
    }

    private fun updateFollowRelationForUnFollow(item : UserRelationListItem, holder: BindingViewHolder<ItemCommunityFollowBinding>){
        item.relation = FollowRelation.getRelationForUnFollow(item.relation?:"")
        updateRelation(item.relation!!, holder)
        onRelationUpdateClick?.invoke()
    }


}